package org.zjt.netty.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zjt.domain.RpcMsgProto;
import org.zjt.util.InvokeMethodExecuter;


/**
 * Created by zhangjuntao on 2017/3/16.
 */
public class RpcServerHandler extends ChannelInboundHandlerAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(RpcServerHandler.class);


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        RpcMsgProto.msg req = (RpcMsgProto.msg) msg;
        LOGGER.info("Service accept client subscribe req : [" + req.toString() + "]");
        Object result = InvokeMethodExecuter.exec(req);
        ctx.writeAndFlush(InvokeMethodExecuter.resultRpcMsgProto(req,result));
    }



    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();// 发生异常，关闭链路
    }
}
